﻿Public Class AnimAdop



    Dim nombre, especie, observaciones As String
    Dim adoptado As Boolean
    Dim refCli, edad, id As Integer


    Private Sub AnimAdop_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
        Try
          

            Me.ClientesTableAdapter.Fill(Me.NuevoZooDBDataSet.Clientes)

            Me.AnimAdopTableAdapter.Fill(Me.NuevoZooDBDataSet.AnimAdop)


            ConCombo.SelectedIndex = -1
        Catch ex As Exception
            MsgBox("Error al conectar con la base de datos", MsgBoxStyle.Critical, "Error")
        End Try


        ConCombo.SelectedIndex = -1

        If (Login.encargadologin = True) Then
            BElimAnim.Enabled = True
        Else
            BElimAnim.Enabled = False

        End If

    End Sub



    Private Sub Animales_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Me.CenterToScreen()
    End Sub

   

    Private Sub TxtNombre_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNombre.TextChanged
        Me.AnimAdopTableAdapter.FillByNombre(Me.NuevoZooDBDataSet.AnimAdop, TxtNombre.Text)

    End Sub



    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConCombo.SelectedIndexChanged

    End Sub




    Private Sub TxtElNom_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtElNom.TextChanged
        Me.AnimAdopTableAdapter.FillByNombre(Me.NuevoZooDBDataSet.AnimAdop, TxtElNom.Text)
    End Sub

    Private Sub TxtEdNom_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtEdNom.TextChanged
        Me.AnimAdopTableAdapter.FillByNombre(Me.NuevoZooDBDataSet.AnimAdop, TxtEdNom.Text)
    End Sub


    Private Sub DataGridView4_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView4.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView4.SelectedRows(0)
        id = dr.Cells(0).Value
        LbNom.Text = dr.Cells(1).Value.ToString
        LbEsp.Text = dr.Cells(2).Value.ToString
        LbFec.Text = dr.Cells(3).Value.ToString.Replace("0:00:00", "")
        LblCli.Text = dr.Cells(4).Value.ToString
        LblObs.Text = dr.Cells(5).Value.ToString
    End Sub


    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView2.SelectedRows(0)
        TxtCli.Text = dr.Cells(1).Value.ToString
        refCli = dr.Cells(0).Value
    End Sub



    Private Sub BAddAnim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BAddAnim.Click
        nombre = TxtNom.Text
        especie = ComboEsp.Text
        edad = TxtEdad.Text
        If CheckBox1.Checked = True Then
            adoptado = True
        Else
            adoptado = False

        End If
        observaciones = TxtObs.Text

        Dim query As IEnumerable(Of Decimal) = _
                From item As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
                Where item.Cells(0).Value IsNot DBNull.Value _
                Select CDec(item.Cells(0).Value)

        ' Obtenemos el valor máximo
        '
        Dim id As Integer = query.ToList.Max() + 1
        If Login.validar.ValidarNombre(nombre) <> 3 Or nombre = "" Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para dar de alta a un animal", , "Error")
        ElseIf especie = "" Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para dar de alta a un animal", , "Error")
        ElseIf edad = "" Or Login.validar.ValidarInt(edad, 0, 100) Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para dar de alta a un animal", , "Error")
        ElseIf refCli = 0 Or Login.validar.ValidarInt(refCli, 1, 600) <> 3 Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para dar de alta a un animal", , "Error")
        ElseIf id = 0 Then
            MsgBox("Debe seleccionar un registro de la tabla", , "Error")
        Else

            Try
                Me.AnimAdopTableAdapter.Insert(id, nombre, especie, edad, adoptado, refCli, observaciones)
                Me.AnimAdopTableAdapter.Fill(Me.NuevoZooDBDataSet.AnimAdop)
            Catch ex As Exception
                MsgBox("No se pudo guardar los datos")
            End Try
        End If

    End Sub

    Private Sub BElimAnim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BElimAnim.Click

        If nombre <> "" Then
            Dim respuesta As MsgBoxResult = MsgBox("¿Desea eliminar el registro?", MsgBoxStyle.YesNo, "Confirmación")
            If respuesta = MsgBoxResult.Yes Then
                Try
                    Me.AnimAdopTableAdapter.Delete(id)
                    Me.AnimAdopTableAdapter.Fill(Me.NuevoZooDBDataSet.AnimAdop)
                Catch ex As Exception
                    MsgBox("No se pudo borrar el registro", MsgBoxStyle.Critical, "Error")
                End Try

            Else

            End If
        Else
            MsgBox("Seleccione un registro para borrar.", MsgBoxStyle.OkOnly, "Datos incorrectos")
        End If
    End Sub


    Private Sub DataGridView3_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView3.SelectedRows(0)
        id = dr.Cells(0).Value
        EdNom.Text = dr.Cells(1).Value.ToString
        EdCli.Text = dr.Cells(4).Value.ToString
        EdObs.Text = dr.Cells(5).Value.ToString


    End Sub

    Private Sub BEdAnim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BEdAnim.Click

        nombre = EdNom.Text
        especie = ComboBox1.Text
        edad = EdEdad.Text

        If CheckBox1.Checked = True Then
            adoptado = True
        Else
            adoptado = False

        End If
        Dim number As Integer
        If (Int32.TryParse(EdCli.Text, number)) = True Then
            refCli = number
        End If

        observaciones = EdObs.Text

        If Login.validar.ValidarNombre(nombre) <> 3 Or nombre = "" Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para modificar un animal", , "Error")
        ElseIf especie = "" Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para modificar un animal", , "Error")
        ElseIf refCli = 0 Then
            MsgBox("Debe rellenar todos los campos" & Chr(13) & "para modificar un animal", , "Error")

        Else


            Try
                Me.AnimAdopTableAdapter.UpdateQuery(nombre, especie, edad, adoptado, refCli, observaciones, id)
                Me.AnimAdopTableAdapter.Fill(Me.NuevoZooDBDataSet.AnimAdop)
            Catch ex As Exception
                MsgBox("No se pudo guardar los datos")
            End Try
        End If

    End Sub

    Private Sub NuevoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NuevoToolStripMenuItem.Click
        Consultar.SelectedIndex = 1
    End Sub
    Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click
        Me.Close()
        Gestion.Show()


    End Sub
    Private Sub WebsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WebsToolStripMenuItem.Click

    End Sub
    Private Sub EliminarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Consultar.SelectedIndex = 3
    End Sub
    Private Sub EditarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditarToolStripMenuItem.Click
        Consultar.SelectedIndex = 2
    End Sub

End Class
